package com.sun.huazhi.aitaskservice.controller;

import com.sun.huazhi.aitaskservice.DTO.request.BatchStyleRequest;
import com.sun.huazhi.aitaskservice.DTO.request.CompositionVariantRequest;
import com.sun.huazhi.aitaskservice.DTO.request.CreateCollectionRequest;
import com.sun.huazhi.aitaskservice.DTO.request.ImageGenerationRequest;
import com.sun.huazhi.aitaskservice.DTO.response.*;
import com.sun.huazhi.aitaskservice.common.config.PythonApiProperties;
import com.sun.huazhi.aitaskservice.common.resilience.ResilientServiceExecutor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Map;

@RestController
@RequestMapping("/api/image")
@Tag(name = "图像处理", description = "提供图像生成、编辑和批量处理功能")
public class ImageController {

    @Autowired
    @Qualifier("pythonApiProperties")
    private PythonApiProperties apiProps;

    @Autowired
    private WebClient webClient;

    @Autowired
    private ResilientServiceExecutor resilientExecutor;

    @PostMapping("/generate-image")
    @Cacheable(value = "image_cache", key = "#prompt", unless = "#result == null")
    @Operation(summary = "基础图像生成",
            description = "根据文本描述生成插花艺术图像")
    public Mono<ResponseEntity<ImageGenerationResponse>> generateImage(@RequestBody ImageGenerationRequest request) {
        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/generate-image")
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(request)
                        .retrieve()
                        .bodyToMono(ImageGenerationResponse.class)
                        .timeout(Duration.ofSeconds(15))
                        .map(ResponseEntity::ok));
    }

    @PostMapping("/generate-enhanced-image")
    @Operation(summary = "增强图像生成",
            description = "生成包含多种变体的高质量插花图像")
    public Mono<ResponseEntity<EnhancedImageGenerationResponse>> generateEnhanced(@RequestBody Map<String, Object> request) {
        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/generate-enhanced-image")
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(request)
                        .retrieve()
                        .bodyToMono(EnhancedImageGenerationResponse.class)
                        .map(ResponseEntity::ok));
    }


    @PostMapping("/edit/vase")
    @Operation(summary = "花瓶替换",
            description = "替换指定图像中的花瓶风格")
    public Mono<ResponseEntity<String>> editVase(@RequestBody Map<String, String> request) {
        return webClient.post()
                .uri(apiProps.getUrl() + "/edit-image-vase")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(request)
                .retrieve()
                .bodyToMono(String.class)
                .map(ResponseEntity::ok);
    }

    @PostMapping("/edit/color")
    @Operation(summary = "花朵颜色修改",
            description = "修改指定图像中的花朵颜色")
    public Mono<ResponseEntity<String>> editColor(@RequestBody Map<String, String> request) {
        return webClient.post()
                .uri(apiProps.getUrl() + "/edit-image-color")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(request)
                .retrieve()
                .bodyToMono(String.class)
                .map(ResponseEntity::ok);
    }

    @PostMapping("/edit/lighting")
    @Operation(summary = "光照效果增强",
            description = "修改指定图像的光照效果")
    public Mono<ResponseEntity<String>> editLighting(@RequestBody Map<String, String> request) {
        return webClient.post()
                .uri(apiProps.getUrl() + "/edit-image-lighting")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(request)
                .retrieve()
                .bodyToMono(String.class)
                .map(ResponseEntity::ok);
    }

    @PostMapping("/batch-styles")
    @Operation(summary = "批量风格生成",
            description = "为同一提示词生成多种不同风格的图像")
    public Mono<ResponseEntity<BatchImageResponse>> batchGenerateStyles(
            @RequestBody BatchStyleRequest request) {

        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/batch-generate-styles")
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(request)
                        .retrieve()
                        .bodyToMono(BatchImageResponse.class)
                        .map(ResponseEntity::ok)
        );
    }

    @PostMapping("/composition-variants")
    @Operation(summary = "构图变体生成",
            description = "生成不同构图方式的插花图像")
    public Mono<ResponseEntity<CompositionResponse>> generateCompositionVariants(
            @RequestBody CompositionVariantRequest request) {

        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/generate-composition-variants")
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(request)
                        .retrieve()
                        .bodyToMono(CompositionResponse.class)
                        .map(ResponseEntity::ok)
        );
    }

    @PostMapping("/create-collection")
    @Operation(summary = "创建图像合集",
            description = "根据提示词创建新的图像合集")
    public Mono<ResponseEntity<ImageCollectionResponse>> createImageCollection(
            @RequestBody CreateCollectionRequest request) {

        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/create-image-collection")
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(request)
                        .retrieve()
                        .bodyToMono(ImageCollectionResponse.class)
                        .map(ResponseEntity::ok)
        );
    }
}
